package com.leonwoodstudio.eventdetectionclient;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.leonwoodstudio.eventdetectionclient.eventmessage.ParsedEventMessageToSave;
import com.leonwoodstudio.eventdetectionclient.utils.AsyncImageLoader;
import com.leonwoodstudio.eventdetectionclient.utils.CallbackImpl;
import com.leonwoodstudio.eventdetectionclient.utils.FileAccessor;
import com.leonwoodstudio.eventdetectionclient.utils.Util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by win7 on 2017/4/8.
 */
public class EventHistoryListItem extends LinearLayout {
    private ImageView pic;
    private TextView event;
    private String[] event_types;
    private AsyncImageLoader loader;
    private MainActivity context;
    private ParsedEventMessageToSave parsed;
    private CallbackImpl callbackImpl;

    public ParsedEventMessageToSave GetParsed() {
        return parsed;
    }

    public EventHistoryListItem(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        View view = LayoutInflater.from(context).inflate(R.layout.event_history_list_item, null);
        pic = (ImageView) view.findViewById(R.id.event_pic);
        event = (TextView) view.findViewById(R.id.event_message);
        addView(view);
        Resources res = context.getResources();
        event_types = res.getStringArray(R.array.event_type);
        loader = new AsyncImageLoader();
        this.context = (MainActivity) context;
        callbackImpl = new CallbackImpl(pic, this.context.handler, parsed);
    }
    public void updateView (ParsedEventMessageToSave parsed) {
        this.parsed = parsed;
        callbackImpl.SetupNewParsed(parsed);
        String event_str = Util.DateToString(Util.StringToDate(parsed.getDate())) + "\n" + parsed.getCamera_idx() + "号摄像机拍下\n" +
                parsed.getLane_inx() + "号车道发生 " + event_types[Integer.valueOf(parsed.getEvent_type())] + " 事件\n" + "坐标:X" + parsed.getCoordinate_x() +
                ", Y" + parsed.getCoordinate_y();
        event.setText(event_str);

        pic.setImageBitmap(null);
        pic.destroyDrawingCache();

        Bitmap cacheImage = loader.loadImage(parsed, callbackImpl);
        if (cacheImage != null) {
            pic.setImageBitmap(cacheImage);
        }
    }
}
